package(default_visibility=["//visibility:public"])

load("//:packages.bzl", "CDK_TARGETS", "MATERIAL_TARGETS", "ROLLUP_GLOBALS")
load("//tools:defaults.bzl", "ng_module", "ng_package")
load("//tools/highlight-files:index.bzl", "highlight_files")
load("//tools/package-docs-content:index.bzl", "package_docs_content")

ng_module(
  name = "examples",
  srcs = glob(["**/*.ts"]) + [":example-module"],
  module_name = "@angular/material-examples",
  assets = glob(["**/*.html", "**/*.css"]),
  deps = [
    "@angular//packages/common",
    "@angular//packages/core",
    "@angular//packages/forms",
    "@matdeps//moment",
    "//src/material-moment-adapter",
  ] + CDK_TARGETS + MATERIAL_TARGETS,
  # Specify the tsconfig that is also used by Gulp. We need to explicitly use this tsconfig
  # because in order to import Moment with TypeScript, some specific options need to be set.
  tsconfig = ":tsconfig-build.json",
)

filegroup(
  name = "example-source-files",
  srcs = glob(["*/*.html", "*/*.css", "*/*.ts"])
)

highlight_files(
  name = "highlighted-source-files",
  srcs = [":example-source-files"]
)

package_docs_content(
  name = "docs-content",
  srcs = {
    # We want to package the guides in to the docs content. These will be displayed
    # in the documentation.
    "//guides": "guides",

    # For the live-examples in our docs, we want to package the highlighted files
    # into the docs content. These will be used to show the source code for examples.
    ":highlighted-source-files": "examples-highlighted",

    # In order to be able to run examples in StackBlitz, we also want to package the
    # plain source files into the docs-content.
    ":example-source-files": "examples-source",

    # Package the overviews for "@angular/material" and "@angular/cdk" into the docs content
    "//src/lib:overviews": "overviews/material",
    "//src/cdk:overviews": "overviews/cdk",

    # Package the API docs for the Material and CDK package into the docs-content
    "//src:api-docs": "api-docs",
  }
)

ng_package(
  name = "npm_package",
  srcs = ["package.json"],
  entry_point = "src/material-examples/public_api.js",
  globals = ROLLUP_GLOBALS,
  deps = [":examples"],
  data = [":docs-content"],
  # TODO(devversion): re-enable once we have set up the proper compiler for the ng_package
  tags = ["manual"],
)

genrule(
  name = "example-module",
  # In case the example-module.ts file is present as an actual source file (e.g. generated
  # through Gulp), we need to exclude it because otherwise the genrule would fail.
  # TODO(devversion): remove this once gulp has been replaced with bazel.
  srcs = glob(["**/*.ts"], exclude = ["example-module.ts"]),
  outs = ["example-module.ts"],
  cmd = """
    # As a workaround for https://github.com/bazelbuild/rules_nodejs/issues/404, we pass the
    # data to the Bazel entry-point through environment variables.
    export _SOURCE_FILES="$(SRCS)"
    export _OUTPUT_FILE="$@"
    export _BASE_DIR="$$(dirname $(location //src/material-examples:index.ts))"

    # Run the bazel entry-point for generating the example module.
    ./$(location //tools/example-module:bazel-bin)
  """,
  tools = ["//tools/example-module:bazel-bin"],
  output_to_bindir = True,
)

